﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using System.Configuration;

namespace HY.WEB.contral
{
    public partial class login : System.Web.UI.UserControl
    {
        public string title2 = ConfigurationManager.AppSettings["title"];
        Boolean opencheck = true;
        protected void Page_Load(object sender, EventArgs e)
        {
            Window1.Title = title2;
            if (!IsPostBack)
            {
                LoadData();
                if (Request.Cookies["OnlineLoginInfo"] != null)
                {
                    cbh.Checked = Convert.ToBoolean(Request.Cookies["OnlineLoginInfo"]["IsRemenber"]);
                    if (cbh.Checked)
                    {
                        this.tbxUserName.Text = Request.Cookies["OnlineLoginInfo"]["UserID"];
                        this.tbxPassword.Text = Request.Cookies["OnlineLoginInfo"]["Password"];
                    }
                }
            }
            //自动填充账号
   // put_acc();

         //试用开关
    // Session["test"] = true; //true 试用 false 为正式使用
            
        }

        void put_acc()
        {
            tbxUserName.Text = "admin";
            tbxPassword.Text = "123456";
            opencheck = false;
        }


        private void LoadData()
        {
            // 创建一个 6 位的随机数并保存在 Session 对象中
            Session["CaptchaImageText"] = GenerateRandomCode();
  
        }

        /// <summary>
        /// 创建一个 6 位的随机数
        /// </summary>
        /// <returns></returns>
        private string GenerateRandomCode()
        {
            string s = String.Empty;
            Random random = new Random();
            for (int i = 0; i < 6; i++)
            {
                s += random.Next(10).ToString();
            }
            return s;
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (opencheck)
            {
                if (tbxCaptcha.Text != Session["CaptchaImageText"].ToString())
                {
                    Alert.ShowInTop("验证码错误！");
                    return;
                }
            }

            if (tbxUserName.Text != "" && tbxPassword.Text != "")
            {
                    

                bool b=BLL.sys.chkpw.chkpassword(tbxUserName.Text, tbxPassword.Text);

                if (b)
                {
                    BLL.HR.sys_user su = new BLL.HR.sys_user();
                    DataTable dsu= su.GetList("lgid='" + tbxUserName.Text+ "'").Tables[0];

                    ExtAspNet.Alert.ShowInTop("成功登录！");
                    Session["safe"] = true;
                    Session["ID"] = tbxUserName.Text;  //用户登陆账号
                 
                    //取权限
                    Session["role"] =BLL.Bllbase.getrole(dsu.Rows[0]["sbh"].ToString().Trim());

                    Session["user"] = dsu.Rows[0]["user_name"].ToString().Trim();//员工姓名
                    Session["bh"] = dsu.Rows[0]["sbh"].ToString().Trim();   //员工编号
                    Session["sid"] = dsu.Rows[0]["sid"].ToString().Trim();    //用户登陆账号

                     //清除当前用户查询条件表
                     BLL.pub.pubwhere.del_where_user(tbxUserName.Text);
                    
                    //提醒和清理即将到期的收藏企业
                    //BLL.BLLfun.delete_sales_get();

                     if (cbh.Checked)setcookie();

                     getMenuID();

                    Response.Redirect("frmmain.aspx");

               }
               else
               {
                   ExtAspNet.Alert.ShowInTop("用户名或密码错误！", MessageBoxIcon.Error);
                   Session["safe"] = false;
               }

            }
            else
            {
                ExtAspNet.Alert.ShowInTop("用户名或密码错误！", MessageBoxIcon.Error);
                Session["safe"] = false;
            }
        }

        void getMenuID()
        {

            string bh = Session["bh"].ToString().Trim();
            string menuSID = "";
            DataTable dt1 = BLL.Bllbase.get_table("user_safe", " ubh='" + bh + "'");
            if (dt1.Rows.Count > 0)
            {
                //XmlDataSource1.DataFile = "~/" + dt.Rows[0]["menu"].ToString().Trim();
                menuSID = dt1.Rows[0]["menu"].ToString().Trim();
                Session["menuSID"] = menuSID;
            }
            else
            {
                ExtAspNet.Alert.ShowInParent("请确认账号已分配权限,或与管理员联系!", "提示", ExtAspNet.MessageBoxIcon.Information, "top.location.href='default.aspx'");
                return;
            }
        }

        void setcookie()
        {
            if (Request.Cookies["OnlineLoginInfo"] != null)
            {
                Response.Cookies["OnlineLoginInfo"].Expires = DateTime.Now.AddDays(365);
                Response.Cookies["OnlineLoginInfo"]["UserID"] = tbxUserName.Text.Trim();
                Response.Cookies["OnlineLoginInfo"]["Password"] = tbxPassword.Text.Trim();
                Response.Cookies["OnlineLoginInfo"]["IsRemenber"] = cbh.Checked.ToString();
            }
            else
            {
                HttpCookie Cookie = new HttpCookie("OnlineLoginInfo");
                Cookie.Expires = DateTime.Now.AddDays(365);
                Cookie.Values.Add("UserID", tbxUserName.Text.Trim());
                Cookie.Values.Add("Password", tbxPassword.Text.Trim());
                Cookie.Values.Add("IsRemenber", cbh.Checked.ToString());               
                Response.AppendCookie(Cookie);
            }
        
        }


    }
}